package start.spring.basic.flowable.service.flowable.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import start.spring.basic.flowable.dao.WfFormMapper;
import start.spring.basic.flowable.entity.WfForm;
import start.spring.basic.flowable.service.BaseServiceImpl;
import start.spring.basic.flowable.service.flowable.IWfFormService;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.util.common.StringUtil;

/**
 * 表单配置Service业务层处理
 * 
 * @author xuyy
 * @date 2023-08-22
 */
@Service
public class WfFormServiceImpl extends BaseServiceImpl implements IWfFormService {
    
    @Autowired
    private WfFormMapper wfFormMapper;

    /**
     * 查询表单配置列表
     * 
     * @param wfForm 表单配置
     * @return 表单配置
     */
    @Override
    public PageResult<WfForm> selectWfFormPageList(PageQuery pageQuery,WfForm wfForm) {
        IPage<WfForm> page = super.getPage(pageQuery);
        QueryWrapper<WfForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(wfForm.getFormId()!=null, WfForm::getFormId, wfForm.getFormId());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfForm.getFormName()), WfForm::getFormName, wfForm.getFormName());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfForm.getRemark()), WfForm::getRemark, wfForm.getRemark());
        //非管理员登录
        if(!super.getSysUser().isAdmin()) {
        	queryWrapper.lambda().eq(WfForm::getCreateBy, super.getSysUser().getUserName());
        }
        page = this.wfFormMapper.selectPage(page, queryWrapper);
        return super.getPageResult(page);
    }
    
    /**
     * 查询全量表单配置
     * 
     * @param wfForm 表单配置
     * @return 表单配置
     */
    @Override
    public List<WfForm> selectWfFormList(WfForm wfForm) {
        QueryWrapper<WfForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(wfForm.getFormId()!=null, WfForm::getFormId, wfForm.getFormId());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfForm.getFormName()), WfForm::getFormName, wfForm.getFormName());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfForm.getRemark()), WfForm::getRemark, wfForm.getRemark());
        //非管理员登录
        if(!super.getSysUser().isAdmin()) {
        	queryWrapper.lambda().eq(WfForm::getCreateBy, super.getSysUser().getUserName());
        }
        return this.wfFormMapper.selectList(queryWrapper);
    }

    /**
     * 新增表单配置
     * 
     * @param wfForm 表单配置
     * @return 结果
     */
    @Override
    public int insertWfForm(WfForm wfForm) {
    	wfForm.setCreateBy(String.valueOf(super.getSysUser().getUserName()));
    	wfForm.setCreateTime(new Date());
        return this.wfFormMapper.insert(wfForm);
    }

    /**
     * 修改表单配置
     * 
     * @param wfForm 表单配置
     * @return 结果
     */
    @Override
    public int updateWfForm(WfForm wfForm) {
    	UpdateWrapper<WfForm> updateWrapper = new UpdateWrapper<>();
    	updateWrapper.lambda().eq(WfForm::getFormId, wfForm.getFormId());
    	//非管理员登录
        if(!super.getSysUser().isAdmin()) {
        	updateWrapper.lambda().eq(WfForm::getCreateBy, super.getSysUser().getUserName());
        }
    	wfForm.setUpdateBy(String.valueOf(super.getSysUser().getUserName()));
    	wfForm.setUpdateTime(new Date());
        return this.wfFormMapper.update(wfForm, updateWrapper);
    }

    /**
     * 批量删除表单配置
     * 
     * @param formIds 需要删除的表单配置主键
     * @return 结果
     */
    @Override
    public int deleteWfFormByFormIds(List<Long> formIds) {
    	QueryWrapper<WfForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(WfForm::getFormId, formIds);
        //非管理员登录
        if(!super.getSysUser().isAdmin()) {
        	queryWrapper.lambda().eq(WfForm::getCreateBy, super.getSysUser().getUserName());
        }
        return this.wfFormMapper.deleteBatchIds(formIds);
    }
	
}
